﻿package com._public._net
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.net.URLRequest;

	/**
	 * ...
	 * @author WZH(shch8.com)
	 */
	public class FaceLibrary extends EventDispatcher
	{
		private var _libraryURL			: String;
		private var _loaded				: Boolean;
		private var _request			: URLRequest;
		private var _loader				: Loader;
		private var _loaderInfo			: LoaderInfo;

		public function FaceLibrary()
		{
			super();
			_request = new URLRequest();
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleLoadComplete )
			_loaded = false;
		}
		/**
		 * 皮肤加载
		 * @param	assetURL
		 */
		public function load( assetURL:String ): void
		{
			_loaded = false;
			_request.url = assetURL;
			_loader.load( _request );
			_libraryURL = assetURL;
		}
		public function get loaded(): Boolean { return _loaded; };
		
		public function get filePath():String { return _libraryURL };
		/**
		 * Button类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getButton( className:String ):SimpleButton
		{
			var Asset:Object = getAsset( className );
			if (Asset!=null){
				var obj:DisplayObject=new Asset();
				return obj as SimpleButton;
			}
			return null;
		}
		/**
		 * Sprite类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getSprite( className:String ): Sprite
		{
			var Asset:Object = getAsset( className );
			if (Asset!=null){
			var obj:DisplayObject=new Asset();
			return obj as Sprite;
			}
			return null;
		}
		/**
		 * MovieClip类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getMovieClip( className:String ): MovieClip
		 {
			var Asset:Object = getAsset( className );
			if (Asset!=null){
				var obj:DisplayObject=new Asset();
				return obj as MovieClip;
			}
			return null;
		}
		/**
		 * Sound类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getSound( className:String ): Sound
		{
			var Asset:Object = getAsset( className );
			if (Asset!=null){
				var obj:Object=new Asset();
				return obj as Sound;
			}
			return null;
		}
		/**
		 * BitmapData类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getImage( className:String ): BitmapData
		{
			var Asset:Object = getAsset( className );
			
			if ( Asset ) return BitmapData( new Asset() );
			return null;
		}
		/**
		 * 皮肤LoaderInfo类型获取
		 */
		public function get loaderInfo():LoaderInfo {
			return _loaderInfo
		}
		public function set loaderInfo(value:LoaderInfo):void {
			_loaded = true;
			_loaderInfo = value;
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		public function getAsset( className:String ): Object
		{
			try
			{
				var Asset:Object = _loaderInfo.applicationDomain.getDefinition( className ) as Object;
			}
			catch ( error:ReferenceError )
			{
				//trace( "未找到皮肤:'" + className + "' 请检查SWF库里是否存在 '" + _libraryURL + "'" );
				return null;
			}
			
			return Asset;
		}
		

		private function handleLoadComplete( event:Event ): void
		{
			
			loaderInfo = _loader.contentLoaderInfo;
		}
		
	}
}
